<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ClassBase as ClassBaseModel;
use App\Services\Admin\Level;
use Illuminate\Http\Request;
use App\Services\Admin\ClassBase;
use App\Services\Admin\ClassBaseNumbering;
use App\Services\Admin\Store;
use App\Http\Requests\Admin\ClassBaseController\doRule;

class ClassBaseController extends Controller
{
    public function index()
    {

        $result = [
            'result'=>ClassBase::lists() ,
            'select_store'=>Store::where_lists(),
            'class_base_numbering'=>ClassBaseNumbering::lists()->get(),
            'store_id'=>\request()->get('store_id'),
            'class_base_numbering_select'=>\request()->get('class_base_numbering'),
            'sear_value'=>\request()->get('sear_value'),
        ];

        return view('admin.class_base.index', $result);
    }

    public function add()
    {

        $result = [
            'select_store'=>Store::where_lists(),
            'select_class_base_numbering'=>ClassBaseNumbering::lists()->get(),
            'select_id' => '',
            'class_base_numbering_id' => '',
            'result' => '',
            'headhunting_commission_level'=>Level::index()->get()
        ];

        return view('admin.class_base.edit',$result);
    }

    public function edit(Request $request, $id)
    {

        $info = ClassBase::info($id);

        $result = [
            'id'=>$id,
            'select_store'=>Store::where_lists(),
            'select_class_base_numbering'=>ClassBaseNumbering::lists()->get(),
            'select_id' => $info->store_id,
            'class_base_numbering_id' => $info->class_base_numbering_id,
            'result'=> $info,
            'headhunting_commission_level'=>Level::index()->get()
        ];

        return view( 'admin.class_base.edit', $result);

    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $image_avatar = !empty($request->file('class_cove')) ? $request->file('class_cove') : [];

            if (!empty($input['id'])) {
                ClassBaseModel::$modify = false;
                $class_info = (new ClassBaseModel)->where('id', $input['id'])->first();
            }

            if(!empty($input['class_cove_is_null']) && empty($image_avatar)) {
                $input['class_cove'] = "";

                if (!empty($input['id'])) {
                    if(!empty($class_info->class_cove)) {
                        if (file_exists("./".$class_info->class_cove)) {
                            unlink("./".$class_info->class_cove);
                        }
                    }
                }

            }else {
                if (!empty($image_avatar)) {

                    $path = $image_avatar->store('/images/' . date('Ymd'));
                    $input['class_cove'] = "/upload/" . $path;

                } else {

                    unset($input['class_cove']);

                }
            }

            unset($input['class_cove_is_null']);

            $class_video = !empty($request->file('class_video')) ? $request->file('class_video') : [];

            if(!empty($input['class_video_is_null']) && empty($class_video)){
                $input['class_video'] = "";
                if (!empty($input['id'])) {
                    if(!empty($class_info->class_video)) {
                        if (file_exists("./".$class_info->class_video)) {
                            unlink("./".$class_info->class_video);
                        }
                    }
                }
            }else{
                if (!empty($class_video)) {

                    $path = $class_video->store('/video/'.date('Ymd'));
                    $input['class_video'] = "/upload/".$path;

                } else {

                    unset($input['class_video']);

                }
            }

            unset($input['class_video_is_null']);

            if (empty($input['id'])) {

                unset($input['id']);

                ClassBase::add($input);

            } else {

                ClassBase::edit($input['id'],$input);

            }

            return self::success();
        }
    }
}
